package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.WorksheetTagDAO;
import com.proactive.webapp.entity.WorksheetTag;

public class WorksheetTagDAOImpl implements WorksheetTagDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void insert(WorksheetTag worksheetTag) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO worksheet_tag(worksheetID, worksheetTag) VALUES(?,?)";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetTag.getWid());
			smt.setString(2, worksheetTag.getTag());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	public List<WorksheetTag> getTop(int top, int worksheetCategoryID) {
		// TODO Auto-generated method stub
		List<WorksheetTag> tagList = new ArrayList<WorksheetTag>();
		String sql = "SELECT worksheetTag,COUNT(*) AS total FROM worksheet_tag a "
				+ "JOIN worksheet b ON a.worksheetID = b.worksheetID "
				+ "JOIN worksheet_category c ON b.worksheetCategoryID = c.worksheetCategoryID "
				+ "WHERE b.worksheetCategoryID = ? GROUP BY HEX(worksheetTag) "
				+ "ORDER BY total DESC LIMIT ?";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetCategoryID);
			smt.setInt(2, top);
			rs = smt.executeQuery();
			while(rs.next()){
				WorksheetTag worksheetTag = new WorksheetTag();
				worksheetTag.setTag(rs.getString("worksheetTag"));
				tagList.add(worksheetTag);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return tagList;
	}


	@Override
	public List<WorksheetTag> getListByWorksheetID(int worksheetID) {
		// TODO Auto-generated method stub
		List<WorksheetTag> tagList = new ArrayList<WorksheetTag>();
		String sql = "SELECT * FROM worksheet_tag WHERE worksheetID = ?";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetID);
			rs = smt.executeQuery();
			while(rs.next()){
				WorksheetTag worksheetTag = new WorksheetTag();
				worksheetTag.setTag(rs.getString("worksheetTag"));
				tagList.add(worksheetTag);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return tagList;
	}
	
	@Override
	public void delete(int worksheetID) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM worksheet_tag WHERE worksheetID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetID);
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}
}
